<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\de\controller;
use app\BaseController;
use app\api\model\v1\Common as apiCommonModel;
use app\api\model\v1\Language as apiLanguageModel;
use think\facade\Lang;
use think\facade\Config;
use think\facade\Cookie;
use think\facade\Db;
use think\facade\View;

class Base extends BaseController{
    public function initialize(){
    	error_reporting(E_ERROR | E_PARSE );
    	// 转为小写字母
        $general = new \common\General();
        $this->lang = $lang = $module = $general->capital_to_underline(app('http')->getName());
		//加载语言包
		Lang::load(app()->getAppPath().'lang'.DS.$lang.'.php');
		Lang::load(app()->getAppPath().'lang'.DS.'custom.php');
		//加载配置文件以获取后台配置，如：config('dataconfig.path');
		Config::load(app()->getBasePath().'admin'.DS.'config'.DS."app.php");
		// 是否启用留言验证码
		View::assign('feedback_captcha',config('captcha.feedback'));
		
		//判断该语言模块是否存在
		$moduleWhere[] = ['mulu','=',$lang]; $moduleWhere[] = ['signid','=',1];
		$moduleLangRs = Db::name('language')->where($moduleWhere)->find();
		if(empty($moduleLangRs)){
			header('HTTP/1.1 301 Moved Permanently');//发出301头部
            header('Location: '.request()->scheme().'://'.request()->host());
		}
		View::assign('langRs',$moduleLangRs);

        // 控制器和方法名
        $this->controller = $general->capital_to_underline(request()->controller());
        $this->action = $general->capital_to_underline(request()->action());
        View::assign('controller',$this->controller);
        View::assign('action',$this->action);
        View::assign('tabledir',$this->controller);
        
		//网站信息
		$host = request()->host(true);
		$this->siteRs = apiCommonModel::siteRs($lang,$host);
		if($this->siteRs['signid'] == 0){//关闭网站
			echo $this->siteRs['closereason'];
			die;
		}
		View::assign('siteRs',$this->siteRs);
		
        $defaultLang = config('lang.default_lang');
		//语言及模块路径变量
        if($defaultLang == $lang){
            $home_path = '/';
        }else{
            $home_path = '/'.$module.'/';
            if(config('global.url.bind_lang') == 1) $home_path = request()->domain().'/';
        }
        View::assign('lang',$lang);
        View::assign('default_lang',$defaultLang);
        View::assign('home_path',$home_path);
        
        /*城市分站*/
		$this->cityRs = apiCommonModel::area_view($host);
		View::assign('cityRs',$this->cityRs);
        // 是否有开启的城市分站
        $cityWhere[] = ['signid','=',1]; $cityWhere[] = ['is_domain','=',1]; $cityWhere[] = ['lang','=',$defaultLang];
        $citySite = Db::name('area')->where($cityWhere)->whereNotNull('bind_domain')->field('abbreviation')->find();
        View::assign('citySite',$citySite);
        
        // 语言列表
        $lang_list = apiLanguageModel::index(1);
        if(!empty($this->cityRs) and $defaultLang == $lang) $lang_list = array();
        View::assign('lang_list',$lang_list);
		
 		//全局参数
		$fieldList = apiCommonModel::field($lang);
		foreach($fieldList as $k => $w){
		    View::assign($w['fieldlabel'],$w['fieldvalue']);
		}
		
		//tp5.1.14起动态配置模板路径改变了，所以使用此笨方法
		// $this->view->config('view_path',config('template.view_path'));
		//tp6.0已在中间件处理
        //获取根域名，分享等用到
		View::assign('root',request()->domain());

		//留言属性
		$gbWhere[] = ['tabledir','=','feedback']; $gbWhere[] = ['parentid','=',0]; $gbWhere[] = ['lang','=',$lang];
		View::assign('gb_attribute',Db::name('attribute')->where($gbWhere)->order('sequence desc')->select());

		//分类下拉展开标志
		$parentArr = array(); $sortid = input('sortid');
		if(is_numeric($sortid)){
			$parentArr = getParentSort(Db::name('sort')->where('lang',$lang)->field('id,parentid')->select(),$sortid);
		}
		View::assign('parent_arr',$parentArr);

		//导航嵌套循环
		$navWhere[] = ['att_type','in','head,tile']; $navWhere[] = ['lang','=',$this->lang];
		$navWhere[] = ['parentid','=',0]; $navWhere[] = ['signid','=',1];
		$client = request()->isMobile() ? 'wap' : 'pc';
		$navigation = sublist('navigation',$navWhere,20,'sequence desc','',$client);
		View::assign('navigation',$navigation);

		//推荐好友
		if(input('?spread')){
			Cookie::set('bmkj_spread',input('spread'),2592000);//有效期1个月
		}
		// 来源url
        if(!cookie('?url_from')){
            $url_from = !empty($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : '';
            Cookie::set('url_from',$url_from);
        }
		
		// 通过Token获取用户ID和会员组类型
        $token = session('token') ? session('token') : (string)input('token');
        $result = checkToken($token, $lang);
        if($result['code'] !== 9000){
            $this->userid = $this->is_admin = '';
        }else{
            $this->userid = $result['data']->userid;
            $this->is_admin = $result['data']->is_admin;
        }
        View::assign('userid',$this->userid); View::assign('is_admin',$this->is_admin);
		
    }
}